package com.red.wood.module.eval.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.inject.internal.Lists;
import com.red.wood.common.dao.ISequnceDao;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.DictionaryDao;
import com.red.wood.module.eval.dao.SingleTaskDao;
import com.red.wood.module.eval.model.SingleTask;
import com.red.wood.module.eval.service.SingleTaskService;
import com.red.wood.module.system.dao.SwUserDao;

@Service
@Transactional
public class SingleTaskServiceImpl implements SingleTaskService {

	@Autowired
	private SingleTaskDao singleTaskDao;
	@Autowired
	private DictionaryDao dictionaryDao;
	@Autowired
	private SwUserDao swUserDao;
	
	@Autowired
	private ISequnceDao sequnceDao;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		return singleTaskDao.getPageData(param);
	}

	public void add(String[] types, String businessScope, String userId) {
		List<Map<String, Object>> paramList = Lists.newArrayList();
		for(String type : types){
			if(StringUtils.isNotBlank(type)){
				Map<String, Object> paramMap = new HashMap<>();
				String form_number = "AQ"+sequnceDao.nextVal("form_number_seq");
				paramMap.put("form_number",form_number);//新增表单编号
				paramMap.put("type", type);
				paramMap.put("businessScope", businessScope);
				paramMap.put("name", dictionaryDao.getValueByTypeAndKey(SingleTask.EVAL_SINGLETASK_NAME, type));
				paramMap.put("applyBusiness", dictionaryDao.getValueByTypeAndKey(SingleTask.EVAL_SINGLETASK_BUSSINESS, type));
				paramMap.put("statue", 1);
				paramMap.put("lastSubmitTime", new Date());
				paramMap.put("createTime", new Date());
				if(StringUtils.isNotBlank(userId)){
					paramMap.put("userId", userId);
				} else {
					paramMap.put("userId", "1");
				}
				paramList.add(paramMap);
			}
		}
		singleTaskDao.add(paramList);
	}

	@Override
	public void delete(String id) {
		singleTaskDao.delete(id);
	}

	@Override
	public void update(IMap paramMap) {
		singleTaskDao.update(paramMap);
	}
	
	public Map<String, Object> getById(String id){
		return singleTaskDao.getById(id);
	}
	
	public Map<String, Object> getUserById(String id){
		try{
			Map<String,Object> singleTask = singleTaskDao.getById(id);
			if(singleTask != null){
				String userId = singleTask.get("userId").toString();
				Map<String, Object> userMap = swUserDao.getById(userId);
				if(userMap != null){
					return userMap;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return null;
	}

}
